package com.fenghongzhang.day016;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MusicUtil {
    public static final int PLAY_MODEL_SINGLE = 0;
    public static final int PLAY_MODEL_RANDOM = 1;
    public static final int PLAY_MODEL_ORDER = 2;

    private static final String TAG = "MusicUtil";

    public static List<Music> getData(Context context){
        List<Music> list = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
        //list
        if (query != null) {
            while (query.moveToNext()) {
                String title = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String duration = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE));
                String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));//
                String albumId = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
//                getPIc(context, albumId);

                //如果时间较短,就不加了
                if(Integer.parseInt(duration) > 1000 * 60){
                    Music music = new Music(title, artist, duration, data, size, albumId);
                    list.add(music);
                }
            }
            query.close();
        }

        return list;
    }

    ///storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1611192833320
    public static String getPIc(Context context, String albumId) {
        //可以通过相册的id 查找到图片的地址,然后让imageview显示.
        ContentResolver contentResolver1 = context.getContentResolver();
        Uri parse = Uri.parse("content://media/external/audio/albums/"+albumId);
        Cursor query1 = contentResolver1.query(parse, null, null, null, null);
        if(query1 != null){
            boolean b = query1.moveToFirst();
            if(b){
                String album_art = query1.getString(query1.getColumnIndex("album_art"));
                return album_art;
            }
        }
        return null;
    }

    //格式化时间 3:17  或者 3:07
    public static String fmtTime(String time){
        //190145
        int i = Integer.parseInt(time);
        if(i / 1000 % 60 < 10){
            return i / 1000 / 60 +":0" + i /1000 % 60;
        }
        return i / 1000 / 60 +":" + i /1000 % 60;
    }

}
